package com.dcm.live.utils;


import java.io.File;

import com.dcm.common.exception.MyException;
import com.vhall.toolkit.VhallUploadKit;
import org.apache.http.util.TextUtils;

import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;

/**
 * @author pengrongsheng
 */
public class UploadUtil{

    private static final long serialVersionUID = 560684569647135515L;
    /**
     * 微吼APPKEY
     */
    public static final String APP_KEY = "3ad75897e762121db321844f8224d456";
    /**
     * 微吼SECRETKEY
     */
    public static final String SECRET_KEY = "28d5d455e79a1f0f55ab0786a828866c";

    /**
     * 子账号为空 默认为主账号
     */
    public static final String USER_ID = "";

    static VhallUploadKit util;
    static String fileKey = "";

    private static void startUpload(String videoName,String subjectName,File file) {
        if (file == null) {
            throw new MyException("请选择文件！");
        }
        util = VhallUploadKit.getInstance();
        util.initData(APP_KEY, SECRET_KEY);
        String key = util.uploadAndBuildWebinar(USER_ID, file, videoName, subjectName, null,
                new PutObjectProgressListener(file.length()));

        if (!TextUtils.isEmpty(key)) {
            fileKey = key;
        }
    }

    /**
     * 获取上传进度回调
     */
    static class PutObjectProgressListener implements ProgressListener {

        private long bytesWritten = 0;
        private long totalBytes = -1;
        private boolean succeed = false;
        private long fileLength = 0;

        public PutObjectProgressListener(long fileLength) {
            super();
            this.fileLength = fileLength;
        }

        @Override
        public void progressChanged(ProgressEvent progressEvent) {
            long bytes = progressEvent.getBytes();
            ProgressEventType eventType = progressEvent.getEventType();
            switch (eventType) {
                case TRANSFER_STARTED_EVENT:
                    System.out.println("开始上传");
                    break;
                case REQUEST_CONTENT_LENGTH_EVENT:
                    this.totalBytes = bytes;
                    this.bytesWritten = fileLength - totalBytes;
                    break;
                case REQUEST_BYTE_TRANSFER_EVENT:
                    this.bytesWritten += bytes;
                    if (this.totalBytes != -1) {
                        int percent = (int) (this.bytesWritten * 100.0 / this.fileLength);
                        System.out.println(bytes + " bytes have been written at this time, upload progress: " + percent
                                + "%(" + this.bytesWritten + "/" + this.fileLength + ")");
                    } else {
                        System.out.println(bytes + " bytes have been written at this time, upload ratio: unknown" + "("
                                + this.bytesWritten + "/...)");
                    }
                    break;

                case TRANSFER_COMPLETED_EVENT:
                    this.succeed = true;
                    System.out.println("上传成功!" + "fileKey=" + fileKey);
                    fileKey = "";
                    break;

                case TRANSFER_FAILED_EVENT:
                    System.out.println("上传失败!");
                    break;

                default:
                    break;
            }
        }

        public boolean isSucceed() {
            return succeed;
        }

        @Override
        public void webinarCreate(String fileKey, String webinarId, String recordId) {
            System.out.println("生成回放成功，活动ID：" + webinarId +
                    "片段ID：" + recordId);
        }

        @Override
        public void onCreateWebinarError(String s, String s1) {
            //上传创建回放时 生成错误
        }

        @Override
        public void onUploadFinal(String s, String s1) {
            //上传结束
        }
    }

}
